package com.lzm.ydpt.module.riding.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.byt.framlib.base.BaseFragment;
import com.byt.framlib.basemvp.boby.FormBodys;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.byt.framlib.commonwidget.refresh.RefreshHeaderView;
import com.lzm.ydpt.Dapplication;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.UserInfo;
import com.lzm.ydpt.module.riding.activity.MyAccountActivity;
import com.lzm.ydpt.module.riding.activity.MyInfoActivity;
import com.lzm.ydpt.module.riding.activity.PerAddressActivity;
import com.lzm.ydpt.mvp.contract.PtMainContract;
import com.lzm.ydpt.mvp.presenterimpl.PtMainPresenterImpl;
import com.lzm.ydpt.view.pop.ContactPopWindows;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.SimpleMultiPurposeListener;

import butterknife.BindView;
import butterknife.OnClick;


public class RidingMineFragment extends BaseFragment<PtMainPresenterImpl> implements ContactPopWindows.ListViewOnImtermClick, PtMainContract.View {
    private static RidingMineFragment sFragment;
    @BindView(R.id.rl_myinfo_layout)
    RelativeLayout rl_myinfo_layout;
    @BindView(R.id.rl_touxiang)
    RelativeLayout rl_touxiang;
    @BindView(R.id.img_touxiang)
    ImageView img_touxiang;
    @BindView(R.id.tv_user_name)
    TextView tv_user_name;
    @BindView(R.id.myinfo_refreshLayout)
    SmartRefreshLayout myinfo_refreshLayout;
    private ContactPopWindows bottomPopWindows;
    private UserInfo tempUser = null;

    public static RidingMineFragment newsInstance() {
        sFragment = new RidingMineFragment();
        return sFragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_riding_mine;
    }

    @Override
    public PtMainPresenterImpl initPresenter() {
        return new PtMainPresenterImpl(this);
    }

    @Override
    public void initFristData() {
        super.initFristData();
//        if (Dapplication.getUserInfo() == null) {
//            fetchUserData();
//        } else {
//            this.tempUser = Dapplication.getUserInfo();
//            initUserData();
//        }
    }

    private void initUserData() {
//        ImageLoaderUtils.displayCircle(img_touxiang,tempUser.get);
        tv_user_name.setText(tempUser.getRealName());
    }

    private void fetchUserData() {
        FormBodys formBodys = new FormBodys.Builder()
                .add("memberId", Dapplication.getUserToken())
                .build();
        mPresenter.getUserInfo(formBodys);
    }

    @Override
    protected void initView() {
        setRefreshLayout(myinfo_refreshLayout);
        myinfo_refreshLayout.setRefreshHeader(new RefreshHeaderView(context).getHeaderStyleStaffWhite());
        myinfo_refreshLayout.setOnMultiPurposeListener(new SimpleMultiPurposeListener() {

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                fetchUserData();
            }
        });
        myinfo_refreshLayout.setEnableNestedScroll(false);
        myinfo_refreshLayout.setEnableLoadMore(false);

    }

    @Override
    public void onDestroyView() {
        sFragment = null;
        super.onDestroyView();
    }

    @OnClick({R.id.rl_touxiang, R.id.tv_user_name, R.id.ll_my_account, R.id.ll_per_address, R.id.ll_contact_service})
    public void onClick(View view) {
        if (AntiShake.check(view.getId())) {
            return;
        }
        switch (view.getId()) {
            case R.id.rl_touxiang:
            case R.id.tv_user_name:
                openActivity(MyInfoActivity.class);
                break;
            case R.id.ll_my_account:
                openActivity(MyAccountActivity.class);
                break;
            case R.id.ll_per_address:
                openActivity(PerAddressActivity.class);
                break;
            case R.id.ll_contact_service:
                if (bottomPopWindows == null) {
                    bottomPopWindows = new ContactPopWindows(context, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT, this);
                }
                bottomPopWindows.showPop(rootView);
                break;
        }
    }

    @Override
    public void onLvOnItem(String title) {
        callPhone(title);
    }

    @Override
    public void onUserInfo(UserInfo mUserInfo) {
        this.tempUser = mUserInfo;
        initUserData();
    }

    @Override
    public void showMessage(String message, String method) {

    }

    @Override
    public void showErrorView(String msg) {

    }
}
